package io

import (
	"encoding/xml"
	"fmt"
)

type servers struct {
	Version string   `xml:"version,attr"`
	Svs     []server `xml:"server"`
}

type server struct {
	ServerName string `xml:"serverName"`
	ServerIP   string `xml:"serverIP"`
}

func TestXML() {
	var str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<servers version=\"1\">\n\t<server>\n\t\t<serverName>Local Web</serverName>\n\t\t<serverIP>172.0.θ.1</serverIP>\n\t</server>\n\t<server>\n\t\t<serverName>Local_DB</serverName>\n\t\t<serverIP>172.0.0.2</serverIP>\n\t</server>\n</servers>"

	v := servers{}
	err := xml.Unmarshal([]byte(str), &v)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("%v\n", v)

	s := servers{Version: "11"}
	arr := []server{
		{ServerName: "local", ServerIP: "1001"},
		{ServerName: "local1", ServerIP: "1002"},
		{ServerName: "local2", ServerIP: "1003"},
	}
	s.Svs = arr

	data, _ := xml.Marshal(s)
	fmt.Println(string(data))

	data2, _ := xml.MarshalIndent(s, "", "    ")
	fmt.Println(string(data2))
}
